package com.tan.lbs;

import java.util.List;
import com.tan.lbs.dao.GameDAO;
import com.tan.lbs.dao.QuestDAO;
import com.tan.lbs.dao.impl.GameDAOImpl;
import com.tan.lbs.dao.impl.QuestDAOImpl;
import com.tan.lbs.entity.Game;
import com.tan.lbs.entity.GameHeader;
import com.tan.lbs.entity.Quest;
import com.tan.lbs.util.Constants;
import com.tan.lbs.util.ResourceReader;
import com.tan.lbs.util.URLs;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class QuestsTabActivity extends ListActivity{
	
	private String gameID;
	List<Quest> quests;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.queststab);
		gameID  = getSharedPreferences(Constants.PREFS_NAME, 0).getString("currentGameID", "");
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	}
	
	@Override
	protected void onResume() {
		super.onStart();
		QuestDAO questDAO = new QuestDAOImpl(this);
		List<Quest> questHeaders = questDAO.retreiveQuests(gameID);
		ArrayAdapter adapter = new QuestListRowAdapter(this,R.layout.questlist_item,questHeaders);
		setListAdapter(adapter);
	}
	
	private class QuestListRowAdapter extends ArrayAdapter<Quest> {



		public QuestListRowAdapter(Context context, int textViewResourceId,
				List<Quest> quests) {
			super(context, textViewResourceId, quests);
			QuestsTabActivity.this.quests = quests;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;

			if (view == null) {
				LayoutInflater vi = (LayoutInflater) getContext()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.questlist_item, null);
			}
			
			int highOrder = QuestsTabActivity.this.getSharedPreferences(Constants.PREFS_NAME, 0).getInt("highestAvailableQuest", 0);
			
			final Quest quest = quests.get(position);
			if(quest.getQuestOrder() > highOrder + 1) {
				view.setVisibility(View.GONE);
			} else {
				view.setVisibility(View.VISIBLE);
			}
			if (quest != null) {
				Bitmap icon = ResourceReader.readImage(QuestsTabActivity.this,
						URLs.gameResourceLoc + gameID + "/"
								+ quest.getIconID());

				ImageView questIcon = (ImageView) view
						.findViewById(R.id.questicon);
				questIcon.setImageBitmap(icon);

				TextView questName = (TextView) view.findViewById(R.id.questname);
				questName.setText(quest.getTitle());
				//Button button = (Button) view.findViewById(R.id.questaction);
				
				String status = quest.getStatus();
				TextView statusView = ((TextView)view.findViewById(R.id.queststatus));
				if (Quest.NOT_CHECKED_IN_STATUS.equals(status)) {
					statusView.setText("Status: Not check in");
					statusView.setTextColor(Color.RED);
					
				} else if (Quest.START_STATUS.equals(status)) {
					((TextView)view.findViewById(R.id.queststatus)).setText("Status: Start");
					statusView.setTextColor(Color.GREEN);
					
				} else if (Quest.COMPLETE_STATUS.equals(status)) {
					((TextView)view.findViewById(R.id.queststatus)).setText("Status: Complete");
					statusView.setTextColor(Color.parseColor("#f7a921"));
				}
			}
			return view;
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		final Quest quest = quests.get(position);
		Intent intent = new Intent();
		intent.putExtra("questID", quest.getQuestID());
		intent.setClass(QuestsTabActivity.this,
				QuestDetailActivity.class);
		QuestsTabActivity.this.startActivity(intent);
	}
}
